package com.zerui.manager.app.manager.factory;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.dahuatech.icc.event.enums.CategoryEnums;
import com.zerui.manager.accessrecord.AccessRecord;
import com.zerui.manager.accessrecord.gateway.AccessRecordGateWay;
import com.zerui.manager.common.util.SpringContextTools;
import com.zerui.manager.web.DeviceTouchService;
import com.zerui.manager.web.vo.DeviceTouchPageVO;

import java.time.LocalDateTime;
import java.util.TimerTask;

/**
 * 异步操作
 */
public class AsyncFactory {


    /**
     * 记录门禁报警数据
     *
     * @param recordJo 门禁报警数据
     * @return 任务task
     */
    public static TimerTask saveAccessData(final JSONObject recordJo) {
        return new TimerTask() {
            @Override
            public void run() {
                final String category = recordJo.getString("category");
                if (category.equals(CategoryEnums.alarm.code)) {
                    JSONObject infoJo = recordJo.getJSONObject("info");
                    JSONObject extendJo = infoJo.getJSONObject("extend"); // 业务扩展信息
                    if (null == extendJo) return;
                    final String personCode = extendJo.getString("personCode");// 人员编号
                    final String deviceCode = extendJo.getString("deviceCode");// 设备编号
                    // 构建记录
                    AccessRecord record = AccessRecord.builder()
                            .createTime(LocalDateTime.now())
                            .time(String.valueOf(infoJo.getLong("alarmDate") * 1000))
                            .mode(extendJo.getString("openTypeStr"))
                            .recordId(extendJo.getString("id"))
                            .deviceId(infoJo.getString("deviceCode")) // 设备ID
                            .deviceCode(infoJo.getString("deviceCode")) // 设备编号
                            .deviceName(extendJo.getString("deviceName"))
                            .passStatus(extendJo.getInteger("openResult") == 1 ? "通行成功" : "通行失败")
                            .personnelId(extendJo.getString("personCode")) // 人员ID
                            .personnelName(extendJo.containsKey("personName") ? extendJo.getString("personName") : "陌生人")
                            .personType(extendJo.containsKey("personName") ? null : "陌生人")
                            .build();
                    // 如果设备编号不为空
                    if (StringUtils.isNotBlank(deviceCode)) {
                        DeviceTouchPageVO deviceTouch = SpringContextTools.getBean(DeviceTouchService.class).getDeviceByDeviceCode(deviceCode);
                        if (null != deviceTouch) {
                            record.setDeviceId(deviceTouch.getDeviceId()); // 设备ID
                            record.setDeviceName(deviceTouch.getDeviceName()); // 设备名称
                            record.setPosition(deviceTouch.getPosition()); // 位置信息
                            record.setAccess((null == deviceTouch.getAccess() || deviceTouch.getAccess().equals(0)) ? "进" : "出");
                        }
                    }
                    // 保存门禁进出数据
                    SpringContextTools.getBean(AccessRecordGateWay.class).addAccessRecord(record);
                }
            }
        };
    }
}
